package com.yn.iotxiao.controller.Device;

import com.yn.iotxiao.Utils.StringUtil;
import com.yn.iotxiao.response.ErrorResult;
import com.yn.iotxiao.response.ResponseResult;
import com.yn.iotxiao.sql.entity.Developer.DeveloperInfo;
import com.yn.iotxiao.sql.services.Developer.DeveloperInfoService;
import io.micrometer.common.util.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@RequestMapping("/developer")
@Slf4j
@RestController
@ResponseResult
public class DeveloperController {

    @Autowired
    DeveloperInfoService dService;

    @PutMapping
    public Object putInfo(@Validated @RequestBody DeveloperInfo request) {


        int total = dService.getTotal("");
        if (total >= 10) {
            return new ErrorResult("最多支持设置10个开发者");
        }
        if (StringUtils.isBlank(request.getAppkey())) {
            request.setAppkey("yniotxiao_" + StringUtil.getRandom(12));
            request.setAppsecret(StringUtil.getUUID());
        }
        int result = dService.insert(request);
        if (result > 0) {
            return  "保存成功";
        }
        return new ErrorResult("保存失败");
    }

    @PostMapping("/list")
    public Object getList() {
        return dService.getListPage("",1,10);
    }

    @DeleteMapping("{serid}")
    public Object deleteWithSerId(@PathVariable int serid) {
        int result =  dService.deleteWithKey(serid);
        if (result > 0) {
            return "删除成功";
        }
        return new ErrorResult("删除失败");
    }


}
